/* Author: alex
The main application class
*/

var App = (function ($, Modernizr) {
    // using
    var ui = sketch.ui,
		cmds = sketch.commands,
		fwk = sketch.framework,
        pages = sketch.ui.pages;
    var divId = "#main";

    var App = function () {
        this.toolbox = null;
        this.commands = {};
        this.pages = [];
        this.activePage = null;
    };

    var registerCommand = function (cmd) {
        this.commands[cmd.id] = cmd;
    };

    var registerCommands = function () {
        registerCommand.call(this, new cmds.CreateButtonCommand(this));
        registerCommand.call(this, new cmds.CreatePhoneCommand(this));
        registerCommand.call(this, new cmds.CreateTextAreaCommand(this));
        registerCommand.call(this, new cmds.CreateContainerCommand(this));
        registerCommand.call(this, new cmds.CreateConfirmationDialogCommand(this));
    };

    var setupPannels = function () {
        var that = this;
        $(document).ready(function () {
            $(".collapsibleContainer").collapsiblePanel();
            var tree = new YAHOO.widget.TreeView("elements_tree");
            tree.render();
            var children = that.activePage.view.layer2.children;
            children.changed.bind(function (event) {
                var root = tree.getRoot();
                root.children = [];
                
                for (var i = 0, max = children.length; i < max; i++) {
                    var el = children[i];
                    var tmpNode = new YAHOO.widget.TextNode({
                        label: el.getType(),
                        expanded: false
                    }, root);
                    
                }

                tree.render();
            });
        });
    }

    var doRendering = function () {
        var that = this;
        requestAnimationFrame(function () { doRendering.call(that); });
        
        this.draw();
    };

    App.prototype.Run = function () {
        if (!Modernizr.canvas) {
            throw {
                name: "Not supported browser",
                description: "HTML5 Canvas is not supported"
            };
        }
        fwk.initializeContextExtensions();

        cmds.registerCommands();

        this.toolbox = new ui.Toolbox('#toolbox', sketch.commands);
        this.toolbar = new ui.Toolbar('#toolbar');

        this.activePage = new pages.iPhonePortraitPage($(divId)[0]);
        this.pages.push(this.activePage);

        var propertyDesigner = new ui.PropertyDesigner("#propertyDesigner", cmds);
        var actionDesigner = new ui.ActionDesigner("#actionDesigner", cmds);
        this.viewObserver = new ui.ViewObserver(propertyDesigner, actionDesigner);
        this.viewObserver.attach(this.activePage.view);

        setupPannels.call(this);
        //this.activePage.view.invalidate();
        doRendering.call(this);

        var that = this;
        $(window).resize(function(){
            that.activePage.view.invalidate();
        });
    };

    App.prototype.draw = function () {
            this.activePage.view.draw();
    };

    App.loaded = fwk.EventHelper.createEvent();

    fwk.Resources.loadGradientFromImage('blue_button','img/g/blue_button.png');
    fwk.Resources.loadGradientFromImage('toolbar','img/g/toolbar.png');
    fwk.Resources.loadGradientFromImage('toolbar_black','img/g/toolbar_black.png');
    fwk.Resources.loadGradientFromImage('toolbar_bottom','img/g/toolbar_bottom.png');
    fwk.Resources.loadGradientFromImage('textbox','img/g/textbox.png');
    fwk.Resources.loadGradientFromImage('progressbar_in','img/g/progress_bar_in.png');
    fwk.Resources.loadGradientFromImage('progressbar_out','img/g/progress_bar_out.png');
    fwk.Resources.loadGradientFromImage('button_toolbar_black','img/g/button_toolbar_black.png');
    fwk.Resources.loadGradientFromImage('button_toolbar_normal','img/g/button_toolbar_normal.png');
    fwk.Resources.loadGradientFromImage('button_toolbar_silver','img/g/button_toolbar_silver.png');
    fwk.Resources.loadGradientFromImage('button_toolbar_normal_disabled','img/g/button_toolbar_normal_disabled.png');
    fwk.Resources.loadGradientFromImage('actionsheet_top','img/g/actionsheet_grey_top.png');
    fwk.Resources.loadGradientFromImage('actionsheet_body','img/g/actionsheet_grey_body.png');
    fwk.Resources.loadGradientFromImage('disclosure_button','img/g/disclosure_button.png');
    fwk.Resources.loadGradientFromImage('dialog','img/g/dialog.png');
    fwk.Resources.loadGradientFromImage('tabbar_top','/img/g/tabbar_top.png');
    fwk.Resources.loadGradientFromImage('picker_background','/img/g/picker_background.png');
    fwk.Resources.loadGradientFromImage('switch_on','/img/g/switch_on.png');
    fwk.Resources.loadGradientFromImage('switch_off','/img/g/switch_off.png');
    fwk.Resources.loadGradientFromImage('switch_handle','/img/g/switch_handle.png');
    fwk.Resources.loadGradientFromImage('slider_below','/img/g/slider_below.png');
    fwk.Resources.loadGradientFromImage('slider_over','/img/g/slider_over.png');
    fwk.Resources.loadGradientFromImage('slider_handle','/img/g/slider_handle.png');
    fwk.Resources.loadGradientFromImage('picker_border','/img/g/picker_border.png');
    fwk.Resources.loadGradientFromImage('segmentedItem_plain','/img/g/segmentedItem.png');
    fwk.Resources.loadGradientFromImage('segmentedItem_plain_active','/img/g/segmentedItem_active.png');
    fwk.Resources.loadGradientFromImage('segmentedItem_bar','/img/g/segmentedItem_bar.png');
    fwk.Resources.loadGradientFromImage('segmentedItem_bar_active','/img/g/segmentedItem_bar_active.png');
    fwk.Resources.loadGradientFromImage('segmentedItem_bezeled','/img/g/segmentedItem_bezeled.png');
    fwk.Resources.loadGradientFromImage('segmentedItem_bezeled_active','/img/g/segmentedItem_bezeled_active.png');
    fwk.Resources.loadGradientFromImage('picker_glass','/img/g/picker_glass.png');
    fwk.Resources.loadGradientFromImage('table_header','/img/g/table_header.png');
    fwk.Resources.loadGradientFromImage('progressbar_in_def','/img/g/progress_bar_in_def.png');
    fwk.Resources.loadGradientFromImage('progressbar_out_def','/img/g/progress_bar_out_def.png');
    fwk.Resources.loadImage('calendar','/img/ios/calendar.png');
    fwk.Resources.loadImage('keyboard_numeric','/img/ios/keyboard_numeric.png');
    fwk.Resources.loadImage('keyboard_symbols','/img/ios/keyboard_symbols.png');
    fwk.Resources.loadImage('keyboard_text','/img/ios/keyboard_text.png');
    fwk.Resources.loadImage('picker_shadow','/img/g/picker_shadow.png');
    fwk.Resources.loadImage('resize_circle','/img/resize_circle.png');
    fwk.Resources.loadImage('google_map','/img/ios/google_map.png');
    fwk.Resources.loadImage('pin_reflection','/img/ios/pin_reflection.png');
    fwk.Resources.loadImage('pin_leg','/img/ios/pin_leg.png');
    fwk.Resources.loadImage('statusbar','/img/ios/statusbar.png');


    fwk.Resources.loadImage('table_background1','/img/g/table_background1.png');

    fwk.Resources.completed = function(){
         // start application
        App.Current = new App();
        App.Current.Run();
        App.loaded();
    };
    return App;
})(jQuery, Modernizr);

